﻿/// <reference path="jquery-1.8.2.js" />
/// <reference path="jquery.mobile-1.2.0.js" />
/// <reference path="msls-1.0.0.js" />

function createBooleanSwitch(element, contentItem, trueText, falseText, nullText, optionalWidth) {
    var $selectElement = $('<select data-role="slider"></select>').appendTo($(element));
    $('<option value="false">' + falseText + '</option>').appendTo($selectElement);
    $('<option value="true">' + trueText + '</option>').appendTo($selectElement);
    //$('<option value="null">' + nullText + '</option>').appendTo($selectElement);

    // Now, after jQueryMobile has had a chance to process the new DOM addition, perform our own post-processing:
    setTimeout(function () {
        var $flipSwitch = $('select', $(element));
        // Set the initial value (using helper function below):
        setFlipSwitchValue(contentItem.value);

        // If the content item changes (perhaps due to another control being
        //     bound to the same content item, or if a change occurs programmatically), 
        //     update the visual representation of the control:
        contentItem.dataBind('value', setFlipSwitchValue);

        $flipSwitch.slider('refresh');

        // Conversely, whenver the user adjusts the flip-switch visually,
        //     update the underlying content item:
        $flipSwitch.change(function () {
            contentItem.value = ($flipSwitch.val() === 'true'); //($flipSwitch.val() == 'null' ? null : ($flipSwitch.val() === 'true')); //
        });

        // To set the width of the slider to something different than the default,
        //    need to adjust the *generated* div that gets created right next to
        //    the original select element.  DOM Explorer (F12 tools) is a big help here.
        if (optionalWidth != null) {
            $('.ui-slider-switch', $(element)).css('width', optionalWidth);
        }

        // Helper function to set the value of the flip-switch (used both during initialization, and for data-binding)
        function setFlipSwitchValue(value) {
            $flipSwitch.val((value) ? 'true' : 'false');
            //$flipSwitch.val(value == null || value == 'null' ? 'null' : (value) ? 'true' : 'false');

            // Because the flip switch has no concept of a "null" value
            //    (or anything other than true/false), ensure that the 
            //    contentItem's value is in sync with the visual representation
            contentItem.value = ($flipSwitch.val() === 'true'); //($flipSwitch.val() == 'null' ? null : ($flipSwitch.val() === 'true')); //
        }
    }, 0);
};

function createMapSwitch(element, contentItem, trueText, falseText, optionalWidth) { //, updateRoute
    var $selectElement = $('<select data-role="slider"></select>').appendTo($(element));
    $('<option value="false">' + falseText + '</option>').appendTo($selectElement);
    $('<option value="true">' + trueText + '</option>').appendTo($selectElement);

    // Now, after jQueryMobile has had a chance to process the 
    //     new DOM addition, perform our own post-processing:
    setTimeout(function () {
        var $flipSwitch = $('select', $(element));

        // Set the initial value (using helper function below):
        setMapSwitchValue(contentItem.value);

        // If the content item changes (perhaps due to another control being
        //     bound to the same content item, or if a change occurs programmatically), 
        //     update the visual representation of the control:
        contentItem.dataBind('value', setMapSwitchValue);

        $flipSwitch.slider('refresh');

        // Conversely, whenver the user adjusts the flip-switch visually,
        //     update the underlying content item:
        $flipSwitch.change(function () {
            contentItem.value = ($flipSwitch.val() === 'true');
            //screen.bingMap.setVisible
            var bingMap = contentItem.screen.findContentItem("bingMap");
            var googleMap = contentItem.screen.findContentItem("googleMap");
            if (bingMap) bingMap.isVisible = (contentItem.value);
            if (googleMap) googleMap.isVisible = (!contentItem.value);
        });

        // To set the width of the slider to something different than the default,
        //    need to adjust the *generated* div that gets created right next to
        //    the original select element.  DOM Explorer (F12 tools) is a big help here.
        if (optionalWidth != null) {
            $('.ui-slider-switch', $(element)).css('width', optionalWidth);
        }

        //===============================================================//

        // Helper function to set the value of the flip-switch
        //     (used both during initialization, and for data-binding)
        function setMapSwitchValue(value) {
            //$flipSwitch.val((value) ? 'true' : 'false');
            $flipSwitch.val((value) ? 'true' : 'false');

            // Because the flip switch has no concept of a "null" value
            //    (or anything other than true/false), ensure that the 
            //    contentItem's value is in sync with the visual representation
            contentItem.value = ($flipSwitch.val() === 'true');
            var bingMap = contentItem.screen.findContentItem("bingMap");
            var googleMap = contentItem.screen.findContentItem("googleMap");
            if (bingMap) bingMap.isVisible = (contentItem.value);
            if (googleMap) googleMap.isVisible = (!contentItem.value);
        }
    }, 0);
};


function geo_error(error) {
    switch (error.code) {
        case error.PERMISSION_DENIED:
            alert("User denied the request for Geolocation.");
            break;
        case error.POSITION_UNAVAILABLE:
            alert("Location information is unavailable.");
            break;
        case error.TIMEOUT:
            alert("The request to get user location timed out.");
            break;
        case error.UNKNOWN_ERROR:
            alert("An unknown error occurred.");
            break;
    }
};

function formatTime(date) {
    return (date.getHours() > 9 ? "" : "0") + date.getHours() + ":" + (date.getMinutes() > 9 ? "" : "0") + date.getMinutes();
};

function attachEnterClosePopup(element, contentItem) {
    $(element).keypress(function (event) {
        if (event.which == 13) {
            contentItem.screen.closePopup();
        }
    });
};

function RefreshCountDown(newValue) {
    var today = new Date();
    //PlaySound();
    if (newValue > today) {
        $('#defaultCountdown').countdown('destroy');
        $('#defaultCountdown').countdown({
            until: newValue,
            format: 'HMS',
            expiryText: '<b>Time to move your car!</b>',
            //onExpiry: PlaySound() //alert("countdown expired!")
        });
    }
}
